package com.jshcbd.modules.asset.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jshcbd.framework.vo.Result;
import com.jshcbd.modules.asset.service.IAssetService;
import com.jshcbd.modules.asset.service.IInStoreageService;
import com.jshcbd.modules.asset.service.IInventoryService;
import com.jshcbd.modules.asset.vo.ChartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Api(tags="图形统计")
@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private IAssetService assetService;

    @Autowired
    private IInStoreageService inStoreageService;

    @Autowired
    private IInventoryService iInventoryService;




    @ApiOperation("状态统计")
    @GetMapping(value = "/state")
    public Result<List<ChartVo>> state(HttpServletRequest req) {
        Result<List<ChartVo>> result = new Result();
        List<ChartVo> data = assetService.pieByState();
        result.setData(data);
        log.info(data.toString());
        return result;
    }


    @ApiOperation("分类统计")
    @GetMapping(value = "/classify")
    public Result<Map> classify(HttpServletRequest req) {
        Result<Map> result = new Result();
        List<ChartVo> typeData = assetService.pieByType();
        List<ChartVo> classessData = assetService.pieByClassess();
        result.setData(new HashMap<String,List>(){{
            put("type",typeData);
            put("classes",classessData);
        }});
        return result;
    }



    @ApiOperation("资产入库数量")
    @GetMapping(value = "/instore/{year}")
    public Result<Map> instore(@PathVariable Integer year, HttpServletRequest req) {
        Result<Map> result = new Result();
        Map<Integer,Object> currYearData = new HashMap();
        assetService.groupByInstoreDate(year).forEach(chartVo -> currYearData.put(((Double)chartVo.getGroupBy()).intValue(),chartVo.getCount()) );
        Map<Integer,Object> preYearData = new HashMap();
        assetService.groupByInstoreDate(year-1).forEach(chartVo -> preYearData.put(((Double)chartVo.getGroupBy()).intValue(),chartVo.getCount()) );
        result.setData(new HashMap<String,Map>(){{
            put("curr",currYearData);
            put("pre",preYearData);
        }});

        return result;
    }

    @ApiOperation("代办")
    @GetMapping(value = "/upcomingList")
    public Result<Map<String,Integer>> upcomingList(){
        Result<Map<String,Integer>> result = new Result();

        Map<String,Integer> map = new HashMap<>();

        //入库审批
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        Integer instoreCount = inStoreageService.count(queryWrapper);
        map.put("instoreCount",instoreCount);

        //盘点
        queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        Integer inventoryCount = iInventoryService.count(queryWrapper);
        map.put("inventoryCount",inventoryCount);

        //处置
        queryWrapper = new QueryWrapper();
        queryWrapper.ne("state",3);
        queryWrapper.lt(" DATE_PART('DAY',USEFUL_LIFE-NOW()) ",7);
        Integer disposeCount =  assetService.count(queryWrapper);
        map.put("disposeCount",inventoryCount);

        //归还
        queryWrapper = new QueryWrapper();
        queryWrapper.ne("state",3);
        queryWrapper.lt(" DATE_PART('DAY',DUE_DATE-NOW()) ",7);
        Integer dueCount =  assetService.listView(queryWrapper).size();
        map.put("dueCount",dueCount);


        result.setData(map);
        return result;
    }



}
